package com.dongdong.controller;

import com.dongdong.model.TbSpecification;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.service.SpecService;
import com.dongdong.vo.SpecVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 分页模糊查询
     * @param bean
     * @return
     */
    @PostMapping("/FindSpecList")
    public Result FindSpecList(@RequestBody PageBean<SpecVO> bean){
        PageResult<SpecVO> pageResult = specService.FindSpecList(bean);
        return Result.OK().setData(pageResult);
    }

    /**
     * 根据ID回显规格
     * @param id
     * @return
     */
    @GetMapping("/selectSpecById")
    public Result selectSpecById(@RequestParam Long id){
        SpecVO specVO = specService.selectSpecById(id);
        return Result.OK().setData(specVO);
    }

    /**
     * 增加修改规格
     * @param specVO
     * @return
     */
    @PostMapping("/AddAndUpdateSpec")
    public Result AddAndUpdateSpec(@RequestBody SpecVO specVO){
        //判断是否有ID
        if (specVO.getId() == null){
            //增加规格
            specService.InsertSpec(specVO);
        }else {
            //修改规格
            specService.UpdateSpec(specVO);
        }
        return Result.OK();
    }

    /**
     * 删除规格
     * @param id
     * @return
     */
    @DeleteMapping("/DeleteSpecById")
    public Result DeleteSpecById(@RequestParam Long id){
        specService.DeleteSpecById(id);
        return Result.OK();
    }

    /**
     * 查询全部规格
     * @return
     */
    @GetMapping("/FindBrandAll")
    public Result FindBrandAll(){
        List<TbSpecification> specifications = specService.FindBrandAll();
        return Result.OK().setData(specifications);
    }
}
